package com.lanou.musicbox.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.lanou.musicbox.R;
import com.lanou.musicbox.base.BaseActivity;
import com.lanou.musicbox.utils.MyConfig;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners;
import com.umeng.socialize.exception.SocializeException;
import com.umeng.socialize.sso.UMQQSsoHandler;

import java.util.Map;
import java.util.Set;

/**
 * Created by dllo on 15/11/2.
 */
public class LandingActivity extends BaseActivity implements View.OnClickListener {
    private ImageView land_qq;
    private ImageView land_sina;
    private Context mContext;//定义个承接上下文的全局变量
    private ImageView landing_back;
    private SharedPreferences iSharedPreferences;
    /**
     * 第三方登陆
     **/
    UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.login");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.landing_main);
        land_qq = (ImageView) findViewById(R.id.land_qq);
        land_sina = (ImageView) findViewById(R.id.land_sina);
        landing_back = (ImageView) findViewById(R.id.landing_back);
        landing_back.setOnClickListener(this);
        land_qq.setOnClickListener(this);
        land_sina.setOnClickListener(this);
        mContext = LandingActivity.this;
        //参数1为当前Activity， 参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.
        // 添加QQ平台
//        UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(this, "100424468",
//                "c7394704798a158208a74ab60104f0ba");
        UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(this, "1104821131",
                "fCXTjKTXhJVGszNK");
        qqSsoHandler.addToSocialSDK();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.land_qq:
                mController.doOauthVerify(mContext, SHARE_MEDIA.QQ, new SocializeListeners.UMAuthListener() {
                    @Override
                    public void onStart(SHARE_MEDIA platform) {
                        Toast.makeText(mContext, "授权开始", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(SocializeException e, SHARE_MEDIA platform) {
                        Toast.makeText(mContext, "授权错误", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete(Bundle value, final SHARE_MEDIA platform) {
                        Toast.makeText(mContext, "授权完成", Toast.LENGTH_SHORT).show();
                        //获取相关授权信息
                        mController.getPlatformInfo(LandingActivity.this, SHARE_MEDIA.QQ, new SocializeListeners.UMDataListener() {
                            @Override
                            public void onStart() {
                                Toast.makeText(LandingActivity.this, "获取平台数据开始...", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onComplete(int status, Map<String, Object> info) {
                                if (status == 200 && info != null) {
                                    StringBuilder sb = new StringBuilder();
                                    Set<String> keys = info.keySet();
                                    for (String key : keys) {
                                        sb.append(key + "=" + info.get(key).toString() + "\r\n");
                                    }
                                    /**
                                     * 1.没放值时先清空
                                     * 2.在发送请求
                                     * **/

                                    MyConfig.deleteSharePreAll(mContext,"platform");
                                    MyConfig.putSharePre(mContext,"platform","plant","QQ");


                                    String icon = info.get("profile_image_url").toString();//获得图片对象
                                    String name = info.get("screen_name").toString();//获得名字对象
                                    String gender=info.get("gender").toString();//性别
                                    String province=info.get("province").toString();
                                    String city=info.get("city").toString();
                                    //发送请求
                                    Intent intent = new Intent();
                                    intent.putExtra("profile_image_url", icon);
                                    intent.putExtra("screen_name", name);
                                    intent.putExtra("gender",gender);
                                    intent.putExtra("province", province);
                                    intent.putExtra("city",city);
                                    setResult(100, intent);
                                    MyConfig.putSharePre(mContext, "profile_image_url", "profile_image_url", icon);
                                    MyConfig.putSharePre(mContext, "screen_name", "screen_name", name);
                                    MyConfig.putSharePre(mContext, "gender", "gender", gender);
                                    MyConfig.putSharePre(mContext, "province", "province", province);
                                    MyConfig.putSharePre(mContext, "city", "city", city);
                                    finish();
                                    Log.i("sysom",gender+"--- "+icon+"-- "+province+" --"+city+"-- "+name+"--");
                                    Log.d("TestData", sb.toString());

                                } else {
                                    Log.d("TestData", "发生错误：" + status);
                                }
                            }
                        });
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA platform) {
                        Toast.makeText(mContext, "授权取消", Toast.LENGTH_SHORT).show();
                    }
                });
                break;
            case R.id.land_sina:
                break;
            case R.id.landing_back:
                finish();
                break;
        }

    }
}
